/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package phanTrang;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 *
 * @author Hoang Quy Hong An
 */
public class phantrang extends SimpleTagSupport {

    private int total;
    private String cursor;
    private int realEstateStyleID;
    private int realEstateTypeID;

    public String getCursor() {
        return cursor;
    }

    public int getRealEstateStyleID() {
        return realEstateStyleID;
    }

    public int getRealEstateTypeID() {
        return realEstateTypeID;
    }

    public int getTotal() {
        return total;
    }
    /**
     * Called by the container to invoke this tag. 
     * The implementation of this method is provided by the tag library developer,
     * and handles all tag processing, body iteration, etc.
     */
    @Override
    public void doTag() throws JspException {
        JspWriter out = getJspContext().getOut();
        int cursor1;
        try {
            try {
                cursor1 = Integer.parseInt(cursor);
                
            } catch (NumberFormatException ex) {
                cursor1 = 1;
            }
            int b = getPage(total);
            String str = "";
            if ((cursor1 > 3) && (cursor1 < (b - 3))) {
                if (cursor1 == 4) {
                    str += "<a href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + (b - (b - 1)) + "\">Trang Dau </a>";

                } else {
                    str += "<a href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + (b - (b - 1)) + "\">Trang Dau </a> ... ";
                }
                str += "<a href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + (cursor1 - 2) + "\">" + (cursor1 - 2) + "</a> " + "<a href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + (cursor1 - 1) + "\">" + (cursor1 - 1) + "</a> " + "<a style=\"color:red\">" + cursor1 + "</a> "
                        + "<a href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + (cursor1 + 1) + "\">" + (cursor1 + 1) + "</a> " + "<a href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + (cursor1 + 2) + "\">" + (cursor1 + 2) + "</a> ... " + "<a href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + b + "\"> Trang Cuoi </a>";
            } else if (cursor1 <= 3) {
                if (b <= 5) {

                    for (int i = 1; i <= b; i++) {
                        if (i == 1) {
                            if (cursor1 == i) {
                                str += "<a style=\"color:red\" href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + i + "\">Trang Dau </a> ";
                            } else {
                                str += "<a href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + i + "\">Trang Dau </a> ";
                            }
                        } else if (i == b) {
                            if (cursor1 == i) {
                                str += " <a style=\"color:red\" href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + i + "\"> Trang Cuoi </a>";
                            } else {
                                str += " <a href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + i + "\"> Trang Cuoi </a>";
                            }

                        } else {
                            if (cursor1 == i) {
                                str += " <a style=\"color:red\" href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + i + "\">" + i + "</a> ";
                            } else {
                                str += " <a href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + i + "\">" + i + "</a> ";
                            }
                        }
                    }
                } else {
                    for (int i = 1; i <= 5; i++) {
                        if (i == 1) {
                            if (cursor1 == i) {
                                str += "<a style=\"color:red\" href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + i + "\">Trang Dau </a> ";
                            } else {
                                str += "<a href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + i + "\">Trang Dau </a> ";
                            }

                        } else {
                            if (cursor1 == i) {
                                str += " <a style=\"color:red\" href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + i + "\">" + i + "</a> ";
                            } else {
                                str += " <a href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + i + "\">" + i + "</a> ";
                            }


                        }
                    }
                    str += " .... <a href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + b + "\">  Trang Cuoi </a>";

                }
            } else if (cursor1 >= b - 3) {
                if (b <= 5) {

                    for (int i = 1; i <= b; i++) {
                        if (i == 1) {
                            if (cursor1 == i) {
                                str += "<a style=\"color:red\" href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + i + "\">Trang Dau </a>";
                            } else {
                                str += "<a href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + i + "\">Trang Dau </a>";
                            }

                        } else if (i == b) {
                            if (cursor1 == i) {
                                str += " <a style=\"color:red\" href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + i + "\"> Trang Cuoi </a>";
                            } else {
                                str += " <a href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + i + "\"> Trang Cuoi </a>";
                            }

                        } else {
                            if (cursor1 == i) {
                                str += " <a style=\"color:red\" href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + i + "\">" + i + "</a> ";
                            } else {
                                str += " <a href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + i + "\">" + i + "</a> ";
                            }

                        }
                    }
                } else {
                    str += "<a href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + 1 + "\">Trang Dau </a> ... ";
                    for (int i = cursor1 - 2; i <= b; i++) {

                        if (i == b) {
                            if (cursor1 == i) {
                                str += " <a style=\"color:red\" href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + i + "\"> Trang Cuoi </a>";
                            } else {
                                str += " <a href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + i + "\"> Trang Cuoi </a>";
                            }

                        } else {
                            if (cursor1 == i) {
                                str += " <a style=\"color:red\" href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + i + "\">" + i + "</a> ";
                            } else {
                                str += " <a href=\"?realEstateTypeID="+realEstateTypeID+"&realEstateStyleID="+realEstateStyleID+"&pageno=" + i + "\">" + i + "</a> ";
                            }
                        }
                    }

                }
            }
            out.println(str);
        } catch (java.io.IOException ex) {
            throw new JspException("Error in phantrang tag", ex);
        }
        
    }

    public int getPage(int a) {
        if (a < 10) {
            return 1;
        } else {
            if (a % 10 != 0) {
                return a / 10 + 1;
            } else {
                return a / 10;
            }
        }
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public void setCursor(String cursor) {
        this.cursor = cursor;
    }

    public void setRealEstateStyleID(int realEstateStyleID) {
        this.realEstateStyleID = realEstateStyleID;
    }

    public void setRealEstateTypeID(int realEstateTypeID) {
        this.realEstateTypeID= realEstateTypeID;
    }
    
}
